import { isNull } from './index';

export function queryHelper(values, OPS) {
  const parms = Object.keys(values);
  const querys = [];
  for (let index = 0; index < parms.length; index++) {
    const key = parms[index];
    if (isNull(values[key])) continue;
    let value = values[key];
    let op = OPS.find((e) => e.name === key)?.type;
    const defaultOp = OP.Like;
    if (!isNull(value) && value?.length !== 0) {
      if (value.constructor.name === 'Array' && !op) {
        if (value[0]) {
          querys.push(queryModel(key, value[0], OP.GreateEquals));
        }
        if (value[1]) {
          querys.push(queryModel(key, value[1], OP.LessEquals));
        }
      } else {
        querys.push(queryModel(key, value, op || defaultOp));
      }
    }
  }
  return querys;
}

export function queryModel(field, value, op = OP.Like, relation = relationType.and) {
  return { field, value, op, relation };
}

export const OP = {
  Equals: 1, //等于
  NotEquals: 2, //不等于
  Like: 3, //包含
  Great: 4, //大于
  GreateEquals: 5, //大于等于
  Less: 6, //小于
  LessEquals: 7, //小于等于
  NotLike: 8, //不包含
  IsNull: 9, //为空
  IsNotNull: 10, //不为空
  IN: 11,
  NotIN: 12,
  ArrayIN: 13,
  InSql: 14,
  MultiSelect: 15,
};

export const Order = {
  asc: 1,
  desc: 2,
  gbkasc: 3,
  gbkdesc: 4,
};

export const relationType = {
  and: 1,
  or: 2,
};
